package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:实现多线程方法2 实现Runnable接口
 * User: bjc
 * Date: 2025-08-14
 * Time: 20:03
 */


class MyRunable implements Runnable{
    @Override
    public void run() {

        while (true){
            try {
                System.out.println("线程运行的代码");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public class ThreadDemo3 {
    public static void main(String[] args) {
        //创建MyRunable类的实例
        MyRunable myRunable = new MyRunable();
        //创建Thread类的实例，把MyRunable类的实例作为构造方法参数
        Thread thread = new Thread(myRunable);
        //也可以直接创建Thread类的实例
        Thread thread1 = new Thread(new MyRunable());

        //使用Thread类中的方法start开启线程，才会真正调用系统API，在系统内核中创建出线程
        thread.start();
        while (true){
            System.out.println("hello main");
            try {
                Thread.sleep(1000);//休眠1秒，单位是毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}
